package com.xtjc03.domain.service.impl;

import com.xtjc03.domain.entity.Temperature;
import com.xtjc03.domain.service.TemperatureService;
import com.xtjc03.serial.service.SerialPortServiceImpl;
import com.xtjc03.util.TimeUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;

@Service
public class TemperatureServiceImpl implements TemperatureService {

    private static final String COMPANY = "xtjc03";

    @Autowired
    TimeUtil timeUtil;

    @Autowired
    SerialPortServiceImpl serialPortService;

    @Override
    public Temperature read() {
        return getComMsg("temp2");
    }

    public Temperature getComMsg(String cmd) {
        Temperature temperature = new Temperature();
        String s = serialPortService.sendStringToCom("xtjc03temp", COMPANY + cmd + "\r\n");
        if (s != null) {
            temperature.setValue(s.substring(10));
            temperature.setCurrentDate(timeUtil.formatDate(new Date()));
        }
        return temperature;
    }
}
